
import { _decorator, Component, Node, Sprite, Label, SpriteFrame } from 'cc';
import PlayerData from '../../Script/Common/PlayerData';
import yyComponent from '../../Script/Common/yyComponent';
import { EventType } from '../../Script/GameSpecial/GameEventType';
import { GlobalEnum } from '../../Script/GameSpecial/GlobalEnum';
import { ShopDataType } from '../../Script/GameSpecial/shopDataCfg';
const { ccclass, property } = _decorator;

@ccclass('ShopItem')
export class ShopItem extends yyComponent {
    @property(Sprite)
    icon: Sprite = null;

    @property(Label)
    priceLabel: Label = null;

    @property(Node)
    btn: Node = null;

    _cfg: ShopDataType = null;  //当前商品的配置 不能修改
    _isLock = true; //是否锁住
    _isUse = false;
    _useChangeCb = null;
    _displayCb = null;

    setData(d: {
        data: ShopDataType, sf: SpriteFrame, isLock: boolean,
        isUse: boolean, useChangeCb: Function, displayCb: Function
    }) {
        this._cfg = d.data;
        this._isLock = d.isLock;
        this._isUse = d.isUse;
        this._useChangeCb = d.useChangeCb;
        this._displayCb = d.displayCb;

        this.setBtn();
        //填充数据
        this.priceLabel.string = this._cfg.price + '';
        this.icon.spriteFrame = d.sf;
        this.priceLabel.node.active = this._isLock && this._cfg.getType == 0;
    }

    //按钮显示
    setBtn() {
        let goldNode = this.btn.getChildByName('gold');
        let useNode = this.btn.getChildByName('use');
        let signNode = this.btn.getChildByName('sign');
        let videoNode = this.btn.getChildByName('video');
        let spinNode = this.btn.getChildByName('spin');

        //四种获取方式
        goldNode.active = false;
        signNode.active = false;
        videoNode.active = false;
        spinNode.active = false;
        //
        useNode.active = false;
        this.btn.active = true;

        if (!this._isLock) {
            //已解锁 + 未使用
            useNode.active = !this._isUse;
        } else {
            //未解锁 --获取方式 0-金币 1-视频 2-签到 3-抽奖
            switch (this._cfg.getType) {
                case 0: //金币
                    goldNode.active = true;
                    break;
                case 1: //视频
                    videoNode.active = true;
                    break;
                case 2: //签到
                    signNode.active = true;
                    break;
                case 3: //抽奖
                    spinNode.active = true;
                    break;
                default:
                    break;
            }
        }
        //是否已使用
        this.btn.active = !this._isUse;

    }
    //设置使用状态
    setUseState(id: number) {
        if (this._isLock) return;
        this._isUse = id == this._cfg.id;
        this.setBtn();
    }

    //选中状态
    onChoose() {
        this._displayCb && this._displayCb(this._cfg.id);
    }

    //按钮点击
    onCheckClick() {
        this.emit(EventType.Common.AudioEvent.playClickBtn);

        if (this._isLock) {
            //未解锁->解锁方式
            switch (this._cfg.getType) {
                case 0: //金币
                    //检测金币
                    let curGold = PlayerData.getData("gameData.asset.gold");
                    if (curGold < this._cfg.price) {
                        // this.emit(EventType.Common.SDKEvent.showMsg, '金币不够哦!');
                        this.showUI(GlobalEnum.UI.getGold);
                        return;
                    }

                    //扣除金币
                    this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
                        attribute: "gameData.asset.gold",
                        value: this._cfg.price,
                        mode: "-",
                        emit: true,
                        save: true,
                    });
                    this.priceLabel.node.active = false;
                    this.unLockItem();
                    break;

                case 1: //视频
                    this.emit(EventType.Common.SDKEvent.showVideo, () => {
                        //刷新当前item状态
                        this.unLockItem();
                    })
                    break;

                case 2: //签到
                    //跳转签到UI
                    this.emit(EventType.Common.UIEvent.enter, GlobalEnum.UI.signUI);
                    this.emit(EventType.Common.UIEvent.exit, GlobalEnum.UI.shop);
                    break;

                case 3: //抽奖
                    //跳转抽奖UI
                    this.emit(EventType.Common.UIEvent.enter, GlobalEnum.UI.spin);
                    this.emit(EventType.Common.UIEvent.exit, GlobalEnum.UI.shop);
                    break;

                default:
                    break;
            }
        } else {
            //已解锁 + 未使用
            if (!this._isUse) {
                let useSkin = PlayerData.getData("gameData.useSkin");
                useSkin[this._cfg.type] = this._cfg.id;
                this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
                    attribute: "gameData.useSkin",
                    value: useSkin,
                    mode: "=",
                    emit: true,
                    save: true,
                });
                PlayerData.getPlayerData().carSkin[this._cfg.type].cur = this._cfg.id;
                //切换当前类型的所有item状态
                this._useChangeCb && this._useChangeCb(this._cfg.id);
            }
        }
    }

    //解锁商品
    unLockItem() {
        let shopData = PlayerData.getData("gameData.shopData");
        shopData[this._cfg.type][this._cfg.id] = true;
        this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
            attribute: "gameData.shopData",
            value: shopData,
            mode: "=",
            emit: true,
            save: true,
        });
        PlayerData.getPlayerData().carSkin[this._cfg.type].owned.push(this._cfg.id);
        //刷新当前item状态
        this._isLock = false;
        this.setBtn();
    }

}
